/**
 * This Class creates a iCalendar file with a event gadget for every day
 * 
 * ########## Note: This is deprecated #########
 * To add to your Calendar use this url:
 * http://www.google.com/calendar/render?cid=http://svn.egotec.com:8081/icebeer/gadgetEvents.ics
 * #############################################
 * 
 * @see SidebarGadget
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib.base.EgoException;
import lib.base.EgoSystem;

/**
 * The Class GadgetEvents.
 * 
 * This Program is used to generate a ICS file
 * with a event for every day in the year, so there is a event gadget
 */
public class GadgetEvents extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
       
    /**
     * The Constructor.
     * 
     * @see HttpServlet#HttpServlet()
     */
    public GadgetEvents() {
        super();
    }

	/**
	 * Creating an event for every day of the current year
	 * 
	 * @param request the request
	 * @param response the response
	 * 
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String head = "BEGIN:VCALENDAR\n"
			+ "CALSCALE:GREGORIAN\n"
			+ "METHOD:PUBLISH\n"
			+ "X-WR-CALNAME;VALUE=TEXT:Request holiday\n"
			+ "VERSION:2.0\n\n";
		
		String body = "";
		
		String foot = "END:VCALENDAR\n";

		/*
		 * load configuration file
		 */
		try {
			EgoSystem.loadConfig();
		} catch (Exception e)
		{
			throw new ServletException(EgoException.CONFIG_FILE_NOT_READABLE, e);
		}
		
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR); // current year
		cal.set(year, 0, 1); // 01.01.year

		while (cal.get(Calendar.YEAR) <= year+1)
		{
			/* iterate the current year and
			 * create for every day an event */
			Calendar working = (Calendar) cal.clone();
			// start + end date
			String s = Integer.toString(working.get(Calendar.YEAR))
						+ (working.get(Calendar.MONTH)<9 ? "0": "") + Integer.toString(working.get(Calendar.MONTH)+1)
						+ (working.get(Calendar.DAY_OF_MONTH)<10 ? "0": "") + Integer.toString(working.get(Calendar.DAY_OF_MONTH));
			working.add(Calendar.DAY_OF_YEAR, 1); // next day
			String e = Integer.toString(working.get(Calendar.YEAR))
						+ (working.get(Calendar.MONTH)<9 ? "0": "") + Integer.toString(working.get(Calendar.MONTH)+1)
						+ (working.get(Calendar.DAY_OF_MONTH)<10 ? "0": "") + Integer.toString(working.get(Calendar.DAY_OF_MONTH));
			
			body += "BEGIN:VEVENT\n"
				+ "DTSTART;VALUE=DATE:"+s+"\n"
				+ "DTEND;VALUE=DATE:"+e+"\n"
				+ "SUMMARY:Request holiday\n"
				+ "X-GOOGLE-CALENDAR-CONTENT-TITLE:Request holiday\n"
				+ "X-GOOGLE-CALENDAR-CONTENT-ICON:http://egotec.com/favicon.ico\n"
				+ "X-GOOGLE-CALENDAR-CONTENT-URL:"
					+ EgoSystem.config.get("host").toString()
					+ EgoSystem.config.get("path").toString()
					+ "modules/event/gadget.jsp?d="+s+"\n"
				+ "X-GOOGLE-CALENDAR-CONTENT-TYPE:application/x-google-gadgets+xml\n"
				+ "X-GOOGLE-CALENDAR-CONTENT-WIDTH:200\n"
				+ "X-GOOGLE-CALENDAR-CONTENT-HEIGHT:320\n"
				+ "X-GOOGLE-CALENDAR-CONTENT-GADGET-PREF;NAME=Format:0\n"
				+ "X-GOOGLE-CALENDAR-CONTENT-GADGET-PREF;NAME=Days:1\n"
				+ "END:VEVENT\n\n";
			
			cal.add(Calendar.DAY_OF_YEAR, 1); // next day
		}
		
		PrintWriter out = response.getWriter();
		out.println(head+body+foot);
	}

	/**
	 * Alias for doGet
	 * 
	 * @param request the request
	 * @param response the response
	 * 
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
